package com.formiaomiao.demain.fragment;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;

import com.formiaomiao.demain.R;
import com.formiaomiao.demain.activity.UpdateUserActivity;
import com.formiaomiao.demain.base.BaseUrl;
import com.formiaomiao.demain.model.UserBean;
import com.formiaomiao.demain.util.MessageEvent;
import com.formiaomiao.demain.util.PreferenceUtils;
import com.formiaomiao.demain.util.PreferenceValues;
import com.formiaomiao.demain.util.StaticValues;
import com.formiaomiao.demain.util.ToastUtils;
import com.formiaomiao.demain.util.UseGlideUtils;
import com.formiaomiao.demain.weight.LoadingDialog;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;

import org.greenrobot.eventbus.EventBus;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class UpdateUserNameFragment extends DialogFragment {


    @BindView(R.id.et_update_username)
    EditText etUpdateUsername;
    @BindView(R.id.btn_update_submit)
    Button btnUpdateSubmit;
    Unbinder unbinder;

    LoadingDialog loadingDialog;

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        // request a window without the title
        dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        return dialog;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.update_username_layout, container, false);
        unbinder = ButterKnife.bind(this, v);
        loadingDialog = new LoadingDialog(getActivity());
        return v;
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            DisplayMetrics dm = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
            dialog.getWindow().setLayout((int) (dm.widthPixels * 0.8), (int) (dm.heightPixels * 0.3));
        }
    }

    /**
     * 修改昵称
     *
     * @param name
     */
    public void UpdataHead(final String name) {
        HttpParams params = new HttpParams();
        params.put("id", StaticValues.userid);
        params.put("nickName", name);
        OkGo.<String>post(BaseUrl.USERNICKNAME).headers("Authorization", PreferenceUtils.getString(getActivity(),PreferenceValues.TOKEN)).params(params).tag(this).execute(new StringCallback() {

            @Override
            public void onSuccess(Response<String> response) {
                if (response.body().contains("200")) {
                    ToastUtils.show(getActivity(), "昵称修改成功");
                    EventBus.getDefault().post(new MessageEvent("更改昵称"));
                    StaticValues.nickname = name;
                    dismiss();
                } else {
                    ToastUtils.show(getActivity(), "昵称修改失败");
                }
                loadingDialog.dismiss();
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                loadingDialog.dismiss();
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick(R.id.btn_update_submit)
    public void onViewClicked() {
        if (etUpdateUsername.getText().toString().isEmpty()) {
            ToastUtils.show(getActivity(), "请输入昵称");
        } else {
            UpdataHead(etUpdateUsername.getText().toString());
        }
    }

}
